
/**
 * 用于跟踪一个行为的耗时,和步骤
 */
export class TaskSta {
  private name!:string
  private startTime!:number
  private stepTime!:number
  private endTime!:number
  private stepCount:number = 0;
  constructor(name:string) {
    this.name = name;
    this.startTime = new Date().getTime();
    this.stepTime = this.startTime;
  }

  static DefaultTask:TaskSta = new TaskSta("default");

  getCost() {
    return this.endTime - this.startTime;
  }
  start(msg:string = ""):string {
    this.stepCount++;
    this.startTime = new Date().getTime();
    this.stepTime = this.startTime;
    return `[${this.name}] [${this.stepCount}] [task start] ${msg ?? ""}`;
  }
  end(isSuccess:boolean = true,errMsg:string = ""):string {
    this.stepCount++;
    this.endTime = new Date().getTime();
    return `[${this.name}] [${this.stepCount}] [task end] [cost:${(this.endTime - this.startTime)/1000} s] ${isSuccess?'success':'with error:'}${errMsg ?? ""}`;
  }
  generateLog(stepName:string,content?:string):string {
    return this.step(stepName,content);
  }
  step(stepName:string,content?:string):string {
    this.stepCount++;
    let now = new Date().getTime();
    let cost = now - this.stepTime;
    this.stepTime = now;
    return `[${this.name}] [${this.stepCount}] [${stepName}] [cost:${cost/1000} s] ${content?content:""}`
  }
}